<?php
require(dirname(__FILE__) . '/includes/init1.php');
require(dirname(__FILE__) . '/includes/simple_html_dom.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>管理中心 - 广告来源列表 </title>
<meta name="robots" content="noindex, nofollow">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles/general.css" rel="stylesheet" type="text/css" />
<link href="styles/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
</head>
<body>
 
<h1>
<span class="action-span1"><a target="_top" href="index.php?act=main">管理中心</a> </span><span id="search_id" class="action-span1"> - 广告来源列表 </span>
<div style="clear:both"></div>
</h1>
  <div class="list-div" id="listDiv">
<table cellpadding="3" cellspacing="1">
  <tr>
    <th>排名</th>
	<th>产品名</th>
	<th>SKU</th>
    <th>所在页</th>
    <th>评价总数</th>
    <th>好评数</th>
    <th>中评数</th>
    <th>差评数</th>
	<th>好评率</th>
	<th>商家</th>
  <tr>
<?php
set_time_limit(0);
$starturl = "http://www.360buy.com/products/1320-1585-1599-0-0-0-0-0-0-0-1-1-{1,2}.html";
$urllists = array();
if(preg_match("~\{(\d+),(\d+)\}~",$starturl,$pagenum)){ 
	$pagebegin = intval($pagenum[1]); 
	$pageend = intval($pagenum[2]);
	for(;$pagebegin<=$pageend;$pagebegin++){ 
		$url = str_replace($pagenum[0],$pagebegin,$starturl);
		array_push($urllists,$url); 
		
	} 
}
$i = 1;
$time = time();
$goods_list = array();
$pattern = "http://www.360buy.com/product/{*}.html";
$pattern = preg_quote($pattern);
$pattern = str_replace('\{\*\}','([^\'\">]*)',$pattern);
$pattern = "~".$pattern."~is";
ob_end_flush();//关闭缓存
foreach($urllists as $url){
	$curl = curl_init(); 
	curl_setopt($curl, CURLOPT_URL,$url); 
	curl_setopt($curl, CURLOPT_REFERER, 'http://www.360buy.com'); 
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
	$result = curl_exec($curl); 
	curl_close($curl);

	$html = str_get_html($result);
	foreach($html->find('#plist ul') as $e){
		$content = $e->innertext;
		$html1 = str_get_html($content);
		foreach($html1->find('.p-name') as $j=>$c){
			$goods_list[$j]['products_name']=iconv('GB2312', 'UTF-8',$c->plaintext);
			$products_sn = get_str($c->innertext,'http://www.360buy.com/product/','.html');
			$goods_list[$j]['products_sn']= $products_sn;
		}
	}
	$html->clear();
	$html1->clear();
	unset($html);
	unset($html1);
	$opts = array(
		'http'=>array(
		'method'=>"GET",
		'timeout'=>30,
		)
	);
	$context = stream_context_create($opts);

	foreach($goods_list as $goods){
		$sn = $goods['products_sn'];
		$name = addslashes($goods['products_name']);
		$url = "http://club.360buy.com/clubservice/productcomment-".$sn."-0-0.html";
		$url1 = "http://chat1.360buy.com/api/check.action?pid=".$sn;
	
		$cnt = 0;
		$cnt1 = 0;
		while($cnt < 3 && ($str=@file_get_contents($url,false,$context))===FALSE) $cnt++; 
		while($cnt1 < 3 && ($str1=@file_get_contents($url1,false,$context))===FALSE) $cnt1++;
		if($str&$str1){
			
			$commentcount = get_str($str,'"CommentCount":',',"AverageScore":');
			$goodcount = get_str($str,'"GoodCount":',',"GoodRate":');
			$generalcount = get_str($str,'"GeneralCount":',',"GeneralRate":');
			$poorcount = get_str($str,'"PoorCount":',',"PoorRate":');
			$brankname = get_str($str1,'brandName=encodeURIComponent(encodeURIComponent(\'','\'));if(!stock){stock');
			if(!trim($brankname)){
				$provider = iconv('GB2312', 'UTF-8', '京东商城');
			}else{
				$provider = addslashes(iconv('GB2312', 'UTF-8', $brankname));
			}
			//$ye_num = echo (int)(($i-1)/36+1);		
			//$GLOBALS['db']->query("insert into ".$GLOBALS['ecs']->table('jingdong')." (products_sn,products_name,commentcount,goodcount,generalcount,poorcount,provider,ranking,add_time) values ('".$sn."','".$name."','".$commentcount."','".$goodcount."','".$generalcount."','".$poorcount."','".$provider."','".$i."','".$time."')");
			if($commentcount!=0){
				$good_count_lv = number_format($goodcount/$commentcount*100,2).'%';
			}else{
				$good_count_lv = '0.00%';
			}
			echo '<tr><td>'.$i.'</td><td>'.$name.'</td><td>'.$sn.'</td><td>'.(int)(($i-1)/36+1).
				'</td><td>'.$commentcount.'</td><td>'.$goodcount.'</td><td>'.$generalcount.'</td><td>'.$poorcount.'</td><td>'.$good_count_lv.'</td><td>'.$provider.'</td></tr>';
            flush();
		}
		//插入数据
		unset($str);
		unset($str1);
		$i++;
	}
}


function get_str($str,$start_str,$end_str){  
 
   $start_pos = strpos($str,$start_str)+strlen($start_str);  
   $end_pos = strpos($str,$end_str);  
   $c_str_l = $end_pos - $start_pos;  
   $contents = substr($str,$start_pos,$c_str_l);  
   return $contents;  
} 
?>
   </table>
   </div>
</body>
</html>